﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.ErrorCode;
using SmartHealthcare.Read.Api.Application.Command.Case;
using SmartHealthcare.Read.Api.Application.Command.Department;
using SmartHealthcare.Read.Api.Application.Command.Hospitalbeds;
using SmartHealthcare.Read.Api.Application.Command.PatientList;
using SmartHealthcare.Read.Api.DTo;
using RBAC.Interstructrue;
using Microsoft.EntityFrameworkCore;
using SmartHealthcare.Read.Api.Application.Command.Medicine;

namespace SmartHealthcare.Read.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SmartHealthcareController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly RBACDbContext _dbContext;

        public SmartHealthcareController(IMediator mediator, RBACDbContext dbContext)
        {
            _mediator = mediator;
            _dbContext = dbContext;
        }

        /// <summary>
        /// 测试数据库连接
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> TestDatabaseConnection()
        {
            try
            {
                // 测试数据库连接
                await _dbContext.Database.CanConnectAsync();
                
                // 获取病人表记录数
                var patientCount = await _dbContext.patientLists.CountAsync();
                var registryCount = await _dbContext.inpatientRegistries.CountAsync();
                
                return Ok(new { 
                    message = "数据库连接成功", 
                    patientCount = patientCount,
                    registryCount = registryCount,
                    timestamp = DateTime.Now
                });
            }
            catch (Exception ex)
            {
                return BadRequest(new { 
                    message = "数据库连接失败", 
                    error = ex.Message,
                    timestamp = DateTime.Now
                });
            }
        }

        /// <summary>
        /// 获取所有科室信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<DepartmentDto>>> GetDepartment([FromQuery]GetDepartmentCommand request)
        {
            return _mediator.Send(request);
        }
        /// <summary>
        /// 获取科室及下级所有案例
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<DepartmentWithCasesDto>>> GetDepartmentWithCases([FromQuery] GetDepartmentWithCasesCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 获取所有病房信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<HospitalRoomDto>>> GetHospitalrooms([FromQuery] GetHospitalroomsCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 获取病床信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<HospitalBedDto>>> GetHospitalbeds([FromQuery] GetHospitalbedsCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 获取指定病房的病床信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<HospitalBedDto>>> GetBeds([FromQuery] GetBedsCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 获取病人列表（包含住院登记信息）
        /// </summary>
        /// <param name="request">查询条件</param>
        /// <returns>病人列表</returns>
        [HttpGet]
        public Task<APIResult<List<PatientListDto>>> GetPatientList([FromQuery] GetPatientListCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 获取患者详细信息（通过病人卡号或病房号查询）
        /// </summary>
        /// <param name="request">查询条件</param>
        /// <returns>患者详细信息</returns>
        [HttpGet]
        public Task<APIResult<List<PatientInfoDto>>> GetPatientInfo([FromQuery] GetPatientInfoCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 获取患者住院登记信息（通过病人卡号、姓名或时间范围查询）
        /// </summary>
        /// <param name="request">查询条件</param>
        /// <returns>患者住院登记信息</returns>
        [HttpGet]
        public Task<APIResult<List<PatientRegistryDto>>> GetPatientRegistry([FromQuery] GetPatientRegistryCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 药物智能搜索
        /// </summary>
        /// <param name="request">搜索条件</param>
        /// <returns>药物搜索结果</returns>
        [HttpGet]
        public Task<APIResult<List<MedicineSearchDto>>> SearchMedicine([FromQuery] SearchMedicineCommand request)
        {
            return _mediator.Send(request);
        }
    }
}
